package com.bai.ruiji.Controller;

import com.bai.ruiji.Common.BaseContext;
import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.OrderDetailService;
import com.bai.ruiji.Service.OrdersService;
import com.bai.ruiji.Service.UserService;
import com.bai.ruiji.pojo.Orders;
import com.bai.ruiji.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 27471
 * Time : 2022/10/15 15:54
 */
@RequestMapping("/order")
@RestController
@Slf4j
public class OderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info(orders.toString());
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 显示订单信息
     */
    @RequestMapping("/userPage")
    private R<Page<Orders>> save(@RequestParam int page, int pageSize){
       Long currentId = BaseContext.getCurrentId();
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.in(Orders::getUserId,currentId);
         queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(page1, queryWrapper);
        return R.success(page1);
    }

    /**
     * 管理
     */
    @RequestMapping ("/page")
    public R<Page<Orders>> saveadd(int page, int pageSize){
        Page page1 = new Page(page, pageSize);
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,currentId);
        ordersService.page(page1, queryWrapper);
        return R.success(page1);
    }
}
